package com.ms.game.sendgift.db;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

import com.ms.game.itembox.ChangeItemLister;
import com.ms.game.itembox.Item;
import com.ms.game.itembox.ItemChangeWay;
import com.ms.game.itembox.db.ItemDAO;
import com.ms.game.itembox.db.PlayerItemModel;
import com.ms.game.itembox.message.SSimpleItem;
import com.ms.game.lobby.GameLobbyModule;
import com.ms.game.sendgift.SendGiftProcessor;
import com.ms.game.sendgift.message.SendGiftNotifyMessage;
import com.ms.game.sendgift.message.SendGiftResponse;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.game.common.Helper;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.agent.money.ChangeCoinLister;
import com.xpec.c4.game.datamanager.agent.money.CoinType;
import com.xpec.c4.game.datamanager.agent.money.MoneyChangeWay;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

/**
 * @see SendGiftProcessor#OnSendGiftFinialAcceptCallBack
 */
public class SaveGiftNotifyFinialAsync extends TaskAsyncCommand {

	/**
	 * 帳單ID
	 */
	public long orderID;
	
	/**
	 * 訂單資料
	 */
	public SendGiftNotifyModel model;
	
	/**
	 * 取回的寄送道具清單
	 */
	public ArrayList<PlayerItemModel> sendGiftItems;
	
	public GUID sessionID;
	
	public String playerName;
	
	public Player playerData;
	
	public String playerIP;
	
	public GUID targetGUID;
	
	public Player targetData;
	
	public SendGiftResponse response;
	
	public GameLobbyModule module;
	
	private SendGiftRecordLog logModel;
	
		
	/**
	* 20161003 Sean
	* 為了Web 增加，有摳到都要給值
	*/
	public boolean answer;
	
	private boolean isSaveDone = false;
	
	@Override
	public void asyncExecute() throws Exception {
		
		if(SendGiftDAO.saveOrUpdateGiftNotify(model)){
			isSaveDone = true;
		}
		
		if(targetData == null && answer && isSaveDone){
			
			targetData = PlayerDAO.loadPlayerByGuid(targetGUID);
			ItemDAO.LoadItemDataToPlayer(targetData, getCurrentModuleServer());
		}
		
		//log 記錄
		if(isSaveDone){
			logModel = new SendGiftRecordLog();
			logModel.setOrderID(model.getOrderID());
			logModel.setSendOrderID(model.getSendOrderID());
			logModel.setOrderNumber(model.getOrderNumber());
			logModel.setPlayerGUID(model.getPlayerGUID());
			logModel.setPlayerName(model.getPlayerName());
			logModel.setPlayerIP(playerIP);
			logModel.setTargetGUID(model.getTargetGUID());
			logModel.setTargetName(model.getTargetName());
			logModel.setGoldCoin(model.getGoldCoin());
			logModel.setServiceCharge(model.getServiceCharge());
			logModel.setItems(model.getItems());
			logModel.setStage(model.getStage());
			logModel.setCreateTime(model.getCreateTime());
			logModel.setAnswerTime(model.getAnswerTime());
			logModel.setCompleteTime(model.getFinishTime());
			logModel.setOverTime(model.getOverTime());
			logModel.setSuccess(answer);
			logModel.setFinish(model.isFinish());
			logModel.setTaken(model.isTaken());	
			//SendGiftRecordDAO.saveOrUpdateGiftNotify(logModel);
		}
	}

	/** 
	 * DC callback
	 * {@link GameLobbyModule#sendGiftCallBackFinial(BaseDataInfo, Object...)}
	 */
	@Override
	public void execute() throws Exception {
		
		if(!isSaveDone){
			//記錄失敗
			response.errorCode = ErrorCode.SEND_GIFT_SAVE_ERROR;
			MessageAction.sendMessage(sessionID, MessageTypes.S_GIFT_FINIAL_GIVE_RESPONSE, response);
			return;
		}
		
			HashMap<String, SSimpleItem> items = new HashMap<String, SSimpleItem>();
			for(PlayerItemModel itemModel : sendGiftItems){
				Item item = new Item(itemModel);
				items.put(item.getSn(), item.toClientFormat());
			}
		
			//記錄下要傳送給自己的資訊
			response.orderID = model.getSendOrderID();
			response.returnGoldCoin = model.getGoldCoin();
			response.charge = model.getServiceCharge();
			response.items = items;
			
			//記錄下要傳送給對方的資訊
			SendGiftNotifyMessage notify = new SendGiftNotifyMessage();
			notify.answer = response.answer;
			notify.fromName = playerName;
			notify.orderID = model.getSendOrderID();
			notify.goldCoin = model.getGoldCoin();
			notify.items = items;
		
			ChangeCoinLister coinLister = new ChangeCoinLister();
			ChangeItemLister itemLister = new ChangeItemLister();
			if(model.getItems() != null){
				itemLister.addSendGiftOrderId(model.getOrderID());
				for (PlayerItemModel itemModel : sendGiftItems) {
					itemLister.addItem(itemModel.getSn(), itemModel.getItemId(), itemModel.getCount(),itemModel.getRefineLevel());
				}
			}
			if(answer){
				//有加手續費
				coinLister.addCoin(CoinType.GOLD, new BigDecimal(notify.goldCoin));
				//金錢寫回DC，給對方
				
				module.getPlayerDataAgent().updatePlayerMoneyAndItemToDC(GUID.parseUUID(model.getOrderID()), targetData, 
						coinLister, MoneyChangeWay.ADD_SEND_GIFT,
						itemLister, ItemChangeWay.ADD_SEND_GIFT,
						null, null, null, module, "sendGiftCallBackFinial", 
						GUID.parseUUID(model.getPlayerGUID()), response, targetGUID, notify, logModel);
				
				/*module.getPlayerDataAgent().updatePlayerMoneyToDC(GUID.parseUUID(model.getOrderID()), targetData, coinLister, 
						MoneyChangeWay.ADD_GIFT_RETURN, null, null, module, 
						"sendGiftCallBackFinial", GUID.parseUUID(model.getPlayerGUID()), response, 
						targetGUID, notify, logModel); */
			}else{
				//有加手續費
				BigDecimal goldCoin = Helper.add(new BigDecimal(response.returnGoldCoin), new BigDecimal(response.charge));
				coinLister.addCoin(CoinType.GOLD, goldCoin);
				//金錢寫回DC，給自己
				
				module.getPlayerDataAgent().updatePlayerMoneyAndItemToDC(GUID.parseUUID(model.getOrderID()), playerData, 
						coinLister, MoneyChangeWay.ADD_GIFT_RETURN,
						itemLister, ItemChangeWay.ADD_GIFT_RETURN,
						null, null, null, module, "sendGiftCallBackFinial", 
						GUID.parseUUID(model.getPlayerGUID()), response, targetGUID, notify, logModel);
				
				/*module.getPlayerDataAgent().updatePlayerMoneyToDC(GUID.parseUUID(model.getOrderID()), playerData, coinLister, 
						MoneyChangeWay.ADD_GIFT_RETURN, null, null, module, 
						"sendGiftCallBackFinial", GUID.parseUUID(model.getPlayerGUID()), response, 
						targetGUID, notify, logModel);*/
			}

	}

	public boolean isSaveDone() {
		return isSaveDone;
	}

	public SendGiftRecordLog getLogModel() {
		return logModel;
	}

}
